# 使用一个空的镜像作为基础
FROM scratch

# 添加Alpine Linux v3.5的APK包
ADD alpine-minirootfs-3.5.3-x86_64.tar.gz /

# 设置环境变量
ENV PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# 设置默认命令
CMD ["/bin/sh"]

#设置apk源
RUN  sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

#安装基础工具
RUN  apk update
RUN  apk add --no-cache ca-certificates sudo curl curl-dev htop wget vim tar zip xz gcc libc-dev make tzdata pcre pcre-dev pcre2-dev libstdc++ git bash linux-headers net-tools autoconf dpkg-dev dpkg file g++ pkgconf re2c zlib-dev libtool automake libaio-dev gettext gettext-dev

RUN  apk add openssl libressl libssl1.0 libmcrypt-dev gettext gettext-dev

#添加用户
RUN  adduser engineer -g engineer -D -u 1000 -s /bin/sh && echo "engineer:123456" | chpasswd && echo "engineer ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

#修改时区
RUN  cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

#目录
RUN  mkdir -p /opt/data && mkdir -p /opt/www && mkdir -p /www/wwwlogs
RUN  mkdir -p /root/.ssh
ADD authorized_keys /root/.ssh/authorized_keys

#supervisor
RUN apk add --no-cache supervisor
ADD supervisor /opt/data/supervisor
RUN  mkdir -p /opt/data/supervisor/run && mkdir -p /opt/data/supervisor/log

#ssh
RUN  apk add --no-cache openssh && sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key && ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key && ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key && ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key

#PHP7.0.33
RUN  mkdir -p /opt/data/php
RUN  apk add --no-cache php7 php7-common php7-apache2 php7-apcu php7-bcmath php7-bz2 php7-calendar php7-cgi php7-ctype php7-curl php7-dba php7-xml php7-pear php7-dev php7-doc php7-dom php7-embed php7-enchant php7-exif php7-fpm php7-ftp php7-imap php7-gd php7-gettext php7-gmp php7-iconv php7-intl php7-json php7-ldap php7-litespeed php7-mbstring php7-mcrypt php7-mysqli php7-mysqlnd php7-odbc php7-opcache php7-openssl php7-pcntl php7-pdo php7-pdo_dblib php7-pdo_mysql php7-pdo_odbc php7-pdo_pgsql php7-pdo_sqlite php7-pgsql php7-phar php7-phpdbg php7-posix php7-pspell php7-session php7-shmop php7-snmp php7-soap php7-sockets php7-sqlite3 php7-sysvmsg php7-sysvsem php7-sysvshm php7-tidy php7-wddx php7-xdebug php7-xmlreader php7-xmlrpc php7-xsl php7-zip php7-zlib
ADD php /opt/data/php
RUN  mkdir -p /opt/data/php/tmp && mkdir -p /opt/data/php/run
RUN  ln -s /usr/bin/php7 /usr/bin/php

#nginx
RUN  mkdir -p /opt/data/nginx
RUN  cd /opt/data/nginx && wget -q -O nginx-1.25.4.tar.gz http://nginx.org/download/nginx-1.25.4.tar.gz && tar zxvf nginx-1.25.4.tar.gz && cd nginx-1.25.4 && ./configure --user=engineer --group=engineer --prefix=/opt/data/nginx --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module  && make && make install && rm -rf /opt/data/nginx/nginx-*
RUN  mv /opt/data/nginx/conf/nginx.conf /opt/data/nginx/conf/nginx.conf.bak
ADD nginx/nginx.conf /opt/data/nginx/conf
ADD nginx/conf.d /opt/data/nginx/conf/conf.d
RUN  ln -s /opt/data/nginx/sbin/nginx /usr/bin/nginx

#redis
RUN  mkdir -p /opt/data/redis
RUN  cd /opt/data/redis && wget -q -O redis-4.0.9.tar.gz http://download.redis.io/releases/redis-4.0.9.tar.gz && tar xzf redis-4.0.9.tar.gz && cd redis-4.0.9 && make 1>/dev/null && make PREFIX=/opt/data/redis install && rm -rf /opt/data/redis/redis-*
ADD redis /opt/data/redis
RUN  mkdir -p /opt/data/redis/run
RUN  ln -s /opt/data/redis/bin/redis-cli /usr/bin/redis-cli && ln -s /opt/data/redis/bin/redis-server /usr/bin/redis-server

#php redis
#RUN  mkdir -p /opt/data/redis/redis-ext && cd /opt/data/redis/redis-ext && wget -q https://github.com/phpredis/phpredis/archive/refs/tags/3.1.6.tar.gz -O redis-3.1.6.tar.gz && tar xzf redis-3.1.6.tar.gz && cd phpredis-3.1.6 && /usr/bin/phpize7 && ./configure --with-php-config=/usr/bin/php-config7 && make && make install
RUN  mkdir -p /opt/data/redis/redis-ext
COPY phpredis-3.1.6 /opt/data/redis/redis-ext/phpredis-3.1.6
RUN  cd /opt/data/redis/redis-ext/phpredis-3.1.6 && /usr/bin/phpize7 && ./configure --with-php-config=/usr/bin/php-config7 && make && make install

RUN  echo "extension=redis.so" > /etc/php7/conf.d/20_redis.ini
RUN  cd / && rm -rf /opt/data/redis/redis-ext


#目录和文件权限
RUN  chown -R engineer.engineer /www && chown -R engineer.engineer /opt/data && chown -R engineer.engineer /opt/www && chown engineer.engineer -R /home/engineer
RUN  chmod -R 755 /opt/data/php/log && chmod -R 755 /opt/data/php/tmp && chmod -R 755 /opt/data/supervisor/log && chmod -R 755 /opt/data/supervisor/run && chmod -R 755 /opt/data/redis/log && chmod -R 755 /opt/data/redis/run

COPY  init.sh /init.sh

RUN  chmod a+x /init.sh

# 设置工作目录
WORKDIR /opt/www

ENTRYPOINT ["/init.sh"]

